home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / isc-src / util / misc / trunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-29  |  3.5 KB  |  167 lines

  1. /* trunc - truncate files
  2.  
  3.    trunc [ options ] file ...
  4.  
  5.    -m USER       mail file to USER before truncating or removing
  6.                  (done only if the file is not empty).
  7.  
  8.    -b BYTES      keep only the last BYTES bytes of the file.
  9.  
  10.    -r            remove file (useful with -m option).
  11.  
  12.    -o OWNER      change owner of file to OWNER (must be numeric).
  13.  
  14.    -g GROUP      change group of file to GROUP (must be numeric).
  15.  
  16.    -c MODE       change mode of file to MODE.
  17.  
  18. */
  19.  
  20. static char  sccsid[] = "@(#) trunc.c   1.2   87/03/10 21:32:16";
  21.  
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26.  
  27. #define BUFSIZE   8*1024
  28. #define MAXINT    0x7fffffff
  29.  
  30. /*---------------------------------------------------------------------------*/
  31.  
  32. void mail_file_to_user(filename, user)
  33. char  *filename, *user;
  34. {
  35.  
  36.   FILE * fpin, *fpout;
  37.   char  cmdline[256];
  38.   int  c;
  39.  
  40.   if (!(fpin = fopen(filename, "r"))) return;
  41.   sprintf(cmdline, "/bin/mail %s", user);
  42.   if (!(fpout = popen(cmdline, "w"))) return;
  43.   fprintf(fpout, "To: %s\nSubject: %s\n\n", user, filename);
  44.   while ((c = getc(fpin)) != EOF) putc(c, fpout);
  45.   fclose(fpin);
  46.   pclose(fpout);
  47. }
  48.  
  49. /*---------------------------------------------------------------------------*/
  50.  
  51. void truncate_file(filename, bytes)
  52. char  *filename;
  53. int  bytes;
  54. {
  55.  
  56.   extern long  lseek();
  57.  
  58.   char  buffer[BUFSIZE];
  59.   char  tempfile[256];
  60.   int  fi;
  61.   int  fo;
  62.   unsigned  i;
  63.  
  64.   sprintf(tempfile, "/tmp/trunc%d", getpid());
  65.  
  66.   fi = open(filename, O_RDONLY);
  67.   fo = creat(tempfile, 0600);
  68.   lseek(fi, (long) -bytes, 2);
  69.   while ((i = read(fi, buffer, BUFSIZE)) > 0)
  70.     write(fo, buffer, i);
  71.   close(fi);
  72.   close(fo);
  73.  
  74.   fi = open(tempfile, O_RDONLY);
  75.   fo = creat(filename, 0666);
  76.   while ((i = read(fi, buffer, BUFSIZE)) > 0)
  77.     write(fo, buffer, i);
  78.   close(fi);
  79.   close(fo);
  80.  
  81.   unlink(tempfile);
  82. }
  83.  
  84. /*---------------------------------------------------------------------------*/
  85.  
  86. main(argc, argv)
  87. int  argc;
  88. char  **argv;
  89. {
  90.  
  91.   extern char  *optarg;
  92.   extern int  optind;
  93.   extern long  strtol();
  94.   extern void exit();
  95.  
  96.   char  *filename;
  97.   char  *user = 0;
  98.   int  bytes = MAXINT;
  99.   int  c;
  100.   int  errflag = 0;
  101.   int  group;
  102.   int  mode;
  103.   int  owner;
  104.   int  remove = 0;
  105.   int  set_group = 0;
  106.   int  set_mode = 0;
  107.   int  set_owner = 0;
  108.   struct stat statbuf;
  109.  
  110.   while ((c = getopt(argc, argv, "m:b:ro:g:c:")) != EOF)
  111.     switch (c) {
  112.     case 'm':
  113.       user = optarg;
  114.       break;
  115.     case 'b':
  116.       bytes = atoi(optarg);
  117.       break;
  118.     case 'r':
  119.       remove = 1;
  120.       break;
  121.     case 'o':
  122.       owner = atoi(optarg);
  123.       set_owner = 1;
  124.       break;
  125.     case 'g':
  126.       group = atoi(optarg);
  127.       set_group = 1;
  128.       break;
  129.     case 'c':
  130.       mode = strtol(optarg, 0, 8);
  131.       set_mode = 1;
  132.       break;
  133.     case '?':
  134.       errflag = 1;
  135.       break;
  136.     }
  137.  
  138.   if (errflag) {
  139.     fprintf(stderr, "usage: trunc [-m user] [-b bytes] [-r] [-o owner] [-g group] [-c mode] file ...\n");
  140.     return 2;
  141.   }
  142.  
  143.   for (; optind < argc; optind++) {
  144.     filename = argv[optind];
  145.  
  146.     if (stat(filename, &statbuf)) continue;
  147.     if (set_group && !set_owner) owner = statbuf.st_uid;
  148.     if (set_owner && !set_group) group = statbuf.st_gid;
  149.  
  150.     if (user && statbuf.st_size) mail_file_to_user(filename, user);
  151.  
  152.     if (remove) {
  153.       unlink(filename);
  154.       continue;
  155.     }
  156.     if (bytes <= 0)
  157.       close(creat(filename, 0644));
  158.     else if (bytes < statbuf.st_size)
  159.       truncate_file(filename, bytes);
  160.  
  161.     if (set_mode) chmod(filename, mode);
  162.     if (set_owner || set_group) chown(filename, owner, group);
  163.   }
  164.   exit(0);
  165.   return 0;
  166. }
  167.